package cn.cstech.aijia.sns;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.lidroid.xutils.util.LogUtils;
import com.sina.weibo.sdk.api.share.BaseResponse;
import com.sina.weibo.sdk.api.share.IWeiboHandler;
import com.sina.weibo.sdk.constant.WBConstants;

import cn.cstech.aijia.R;
import cn.cstech.aijia.sns.weibo.WeiBoController;
import cn.cstech.aijia.ui.base.utils.SPUtils;
import cn.cstech.aijia.ui.base.utils.LocalUtils;

public class WBSnsShareResultActivity extends Activity implements IWeiboHandler.Response {
	private WeiBoController mWeiBoController;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mWeiBoController = WeiBoController.getInstance(this);
		mWeiBoController.init(getIntent(), this);
		
		mWeiBoController.setResponse(getIntent(), this);
	}

	/**
	 * @see {@link Activity#onNewIntent}
	 */
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		if(mWeiBoController == null) {
			mWeiBoController = WeiBoController.getInstance(this);
			mWeiBoController.init(getIntent(), this);
		}
		mWeiBoController.setResponse(getIntent(), this);
		
	}

	@Override
	public void onResponse(BaseResponse baseResponse) {
		
		LogUtils.e("loza-WBSnsShareResultActivity-onResponse:" + baseResponse.errCode + ";" + baseResponse.errMsg);
		
		 switch (baseResponse.errCode) {
	        case WBConstants.ErrorCode.ERR_OK:
	        	sendShareResultBroadcast(SnsErrorCode.SUCCESS.ordinal(), null);
	        	LocalUtils.showToast(this, getString(R.string.toast_share_weibo_success));
//				LocalUtils.sendAppendAction(this);
//	        	Message.obtain(myHandler, MESSAGE_TOAST, getString(R.string.toast_share_weibo_success)).sendToTarget();
	            break;
	        case WBConstants.ErrorCode.ERR_CANCEL:
	        	sendShareResultBroadcast(SnsErrorCode.CANCEL.ordinal(), null);
	        	LocalUtils.showToast(this, getString(R.string.toast_share_cancel));
//				LocalUtils.sendAppendAction(this);
//	        	Message.obtain(myHandler, MESSAGE_TOAST, getString(R.string.toast_share_cancel)).sendToTarget();
	            break;
	        case WBConstants.ErrorCode.ERR_FAIL:
	        	sendShareResultBroadcast(SnsErrorCode.FAILED.ordinal(),baseResponse.errMsg);
	        	LocalUtils.showToast(this, getString(R.string.toast_share_fail));
//	        	Message.obtain(myHandler, MESSAGE_TOAST, getString(R.string.toast_share_fail)).sendToTarget();
	            break;
	        }
//		LocalUtils.showToast(this, baseResponse.errMsg);
		finish();
	}

	private void sendShareResultBroadcast(int errorCode,String errorMsg){
		Intent intent = new Intent(SPUtils.ACTION_SHARE_RESULT);
		intent.putExtra(SPUtils.EXTRA_CODE, errorCode);
		intent.putExtra(SPUtils.EXTRA_MESSAGE, errorMsg);
		sendBroadcast(intent);
	}
}
